import time

from selenium.webdriver.common.by import By
from webdriver_helper import get_webdriver

driver = get_webdriver('chrome')

driver.get('https://mail.qq.com/')

# 让页面最大化的方法
driver.maximize_window()

# 强制等待
time.sleep(1)
# 输出/html/body/div[1]/div[1]/a
t1 = driver.find_element(By.XPATH, '//*[@id="login_pictures"]/div[2]/p[1]').text
print(t1)
# 实现自动化操作业务流程

time.sleep(1)
# 找到iframe嵌套第一层
iframe1 = driver.find_element(By.CLASS_NAME, 'QQMailSdkTool_login_loginBox_qq_iframe ')
driver.switch_to.frame(iframe1)

# 第二层
iframe2 = driver.find_element(By.NAME, 'ptlogin_iframe')
driver.switch_to.frame(iframe2)

# 在iframe中执行操作
element_in_iframe = driver.find_element(By.XPATH, '//*[@id="switcher_plogin"]')
element_in_iframe.click()

# 邮箱定位
email = driver.find_element(By.XPATH, '//*[@id="u"]')
email.send_keys("79575415")
# 密码定位
password = driver.find_element(By.XPATH, '//*[@id="p"]')
password.send_keys("lijiehao2024")

# 切换主页面
driver.switch_to.default_content()
# 输出/html/body/div[1]/div[1]/a
t1 = driver.find_element(By.XPATH, '//*[@id="login_pictures"]/div[2]/p[1]').text
print(t1)

# 切换到子页面
driver.switch_to.frame(iframe1)
driver.switch_to.frame(iframe2)
# 定位到登录按钮
driver.find_element(By.XPATH, '//*[@id="login_button"]').click()

# 强制等待
time.sleep(5)
# 切换主页面
driver.switch_to.default_content()
# 独立密码定位
password2 = driver.find_element(By.XPATH, '//*[@id="pwdInput"]')
password2.send_keys("lijiehao2024")
# 登录定位
driver.find_element(By.XPATH, '//*[@id="confirmButton"]').click()
# 强制等待
time.sleep(5)
# 关闭驱动，回收资源
driver.quit()
